class DinoGame {
    // 游戏基础配置
    static CANVAS_WIDTH = 800;
    static CANVAS_HEIGHT = 400;
    static GROUND_HEIGHT = 50;  // 地面高度

    // 恐龙相关配置
    static DINO_WIDTH = 44;     // 恐龙宽度
    static DINO_HEIGHT = 47;    // 恐龙高度
    static DINO_GROUND_OFFSET = 95;  // 恐龙距离地面的距离
    static DINO_START_X = 50;   // 恐龙初始X位置
    static JUMP_VELOCITY = -15; // 跳跃初始速度
    static GRAVITY = 0.8;      // 重力加速度

    // 障碍物相关配置
    static OBSTACLE_WIDTH = 40;
    static OBSTACLE_BASE_HEIGHT = 80;
    static OBSTACLE_MIN_HEIGHT = 60;  // 最小高度
    static OBSTACLE_MAX_HEIGHT = 100; // 最大高度
    static OBSTACLE_MIN_INTERVAL = 30;  // 最小间隔
    static OBSTACLE_SPAWN_CHANCE = 0.03; // 生成概率
    static OBSTACLE_GROUND_OFFSET = 40; // 障碍物距离地面的距离
    static OBSTACLE_GROUP_SPACING = -15; // 增加组内障碍物间距为50

    // 游戏速度相关配置
    static INITIAL_GAME_SPEED = 6;
    static MAX_GAME_SPEED = 10;
    static SPEED_INCREMENT_INTERVAL = 30; // 每30秒增加速度
    static SPEED_INCREMENT = 1;          // 速度增量

    // 云朵相关配置
    static CLOUD_COUNT = 5;
    static CLOUD_WIDTH = 100;
    static CLOUD_HEIGHT = 60;
    static CLOUD_MIN_Y = 20;
    static CLOUD_MAX_Y = 120;
    static CLOUD_MIN_SPEED = 0.1;
    static CLOUD_MAX_SPEED = 0.6;

    // 重启按钮配置
    static RESTART_BTN_WIDTH = 100;
    static RESTART_BTN_HEIGHT = 40;

    // 音频相关配置
    static BGM_PATH = 'bgm.mp3';
    static JUMP_SOUND_PATH = 'jump.mp3';

    // 碰撞检测配置
    static COLLISION_MARGIN_X = 10;  // 水平方向碰撞边距
    static COLLISION_MARGIN_Y = 5;   // 垂直方向碰撞边距

    constructor() {
        this.canvas = document.getElementById('gameCanvas');
        this.ctx = this.canvas.getContext('2d');

        // 游戏状态
        this.isGameOver = false;
        this.score = 0;

        // 恐龙状态
        this.dino = {
            x: DinoGame.DINO_START_X,
            y: this.canvas.height - DinoGame.DINO_GROUND_OFFSET,
            width: DinoGame.DINO_WIDTH,
            height: DinoGame.DINO_HEIGHT,
            jumping: false,
            velocity: 0
        };

        // 重启按钮状态
        this.restartBtn = {
            x: this.canvas.width / 2 - DinoGame.RESTART_BTN_WIDTH / 2,
            y: this.canvas.height / 2 + 20,
            width: DinoGame.RESTART_BTN_WIDTH,
            height: DinoGame.RESTART_BTN_HEIGHT
        };

        // 初始化其他属性
        this.obstacles = [];
        this.clouds = [];
        this.assetsLoaded = false;
        this.lastObstacleTime = 0;
        this.minObstacleInterval = 60;
        this.gameSpeed = DinoGame.INITIAL_GAME_SPEED;
        this.jumpScored = false;
        this.startTime = Date.now();
        this.audioInitialized = false;

        // 初始化游戏
        this.bindEvents();
        this.loadSVGAssets();
        this.loadAudioAssets();
    }

    bindEvents () {
        // 使用箭头函数绑定，避免this指向问题
        this.canvas.addEventListener('click', (e) => this.handleClick(e));
        this.canvas.addEventListener('touchstart', (e) => this.handleTouch(e));
        document.addEventListener('keydown', (e) => this.handleJump(e));

        // 阻止触摸事件的默认行为（防止页面滚动）
        this.canvas.addEventListener('touchmove', (e) => e.preventDefault(), { passive: false });
    }

    loadSVGAssets () {
        // 直接在代码中定义SVG，确保SVG格式正确
        // 修改 loadSVGAssets 方法中的 svgContent
        const svgContent = `<svg width="400" height="400" xmlns="http://www.w3.org/2000/svg">
    <!-- 恐龙身体 -->
    <ellipse cx="200" cy="200" rx="80" ry="40" fill="green" />
    <!-- 恐龙头部 -->
    <circle cx="280" cy="200" r="20" fill="green" />
    <!-- 恐龙尾巴 -->
    <path d="M120,200 C100,180 100,220 80,200" stroke="green" stroke-width="10" fill="none" />
    <!-- 恐龙前腿 -->
    <line x1="200" y1="220" x2="200" y2="260" stroke="green" stroke-width="10" />
    <!-- 恐龙后腿 -->
    <line x1="180" y1="220" x2="180" y2="260" stroke="green" stroke-width="10" />
</svg>`;

        const cactusContent = `
            <svg version="1.1" xmlns="http://www.w3.org/2000/svg" style="display: block;" viewBox="0 0 2048 2048" width="512" height="512">
<path transform="translate(0,0)" fill="rgb(0,0,0)" d="M 842.932 367.083 C 865.619 279.529 905.571 209.511 1005.58 197.009 C 1143.1 185.79 1205.01 307.295 1209.54 428.489 C 1220.03 421.48 1234.9 408.919 1248.29 411.391 C 1260.13 413.575 1268.98 425.813 1266.12 437.756 C 1262.34 453.558 1226.79 472.308 1212.93 481.235 C 1213.04 545.708 1206.7 615.11 1209.54 678.411 C 1221.79 670.327 1240.81 654.395 1256.24 658.68 C 1268.44 662.069 1274.85 675.586 1270.72 687.357 C 1265.88 701.113 1219.58 726.732 1205.64 733.528 C 1206.97 784.449 1203.84 834.279 1202.29 885.01 C 1200.99 927.79 1197.81 990.314 1199.82 1031.71 C 1308.72 1009.31 1316.39 916.667 1324.24 828.997 L 1337.3 680.855 C 1339.76 650.538 1340.78 619.639 1345.04 589.542 C 1359 490.918 1498.39 494.679 1513.66 589.409 C 1517.09 610.743 1510.42 670.436 1506.47 693.064 C 1528.93 680.116 1553.52 692.525 1547.48 715.634 C 1543.41 731.246 1515.45 743.126 1502.32 750.675 C 1499.03 785.658 1494.98 821.696 1495.26 856.808 C 1520.56 831.427 1551.43 844.029 1544.75 869.777 C 1540.67 885.466 1502.54 903.227 1488.51 911.872 C 1486.28 927.089 1486.09 942.66 1484.22 957.963 C 1465.83 1108.77 1349.26 1203.4 1197.8 1198.05 C 1193.21 1224.62 1193.85 1251.31 1193.86 1278.15 L 1193.93 1375.25 L 1188.67 1688.49 C 1188.65 1762.45 1181.08 1827.89 1086.04 1836.81 C 1009.61 1840.07 875.124 1852.18 860.085 1749.34 C 854.49 1711.08 859.181 1635.62 858.315 1593.71 L 854.175 1366.65 C 853.439 1325.3 850.536 1282.92 852.794 1241.66 C 788.873 1218.97 729.075 1212.22 672.925 1170.21 C 605.392 1119.69 573.382 1057.97 561.914 976.94 C 542.711 968.628 487.063 952.272 507.649 921.444 C 512.061 914.835 519.283 913.365 526.522 911.872 C 537.62 914.178 545.406 918.437 555.096 924.254 C 558.145 877.472 551.796 818.354 551.06 770.159 C 532.59 761.283 489.298 743.671 505.885 716.763 C 510.603 709.11 518.352 707.375 526.522 705.681 C 534.591 707.686 541.736 711.777 549.029 715.634 C 549.156 698.637 548.232 681.72 548.086 664.741 C 547.204 562.038 691.362 549.377 715.266 645.606 C 719.581 662.979 717.239 685.87 717.238 704.044 L 717.171 797.484 C 737.241 777.463 767.882 783.003 764.523 808.84 C 762.283 826.068 730.522 846.058 717.171 856.808 C 724.385 997.596 697.342 1010.88 845.895 1065.54 C 848.424 1040.33 846.456 996.075 845.756 970.004 L 834.722 578.259 C 833.191 526.04 831.765 476.045 838.061 423.893 C 825.881 412.601 782.084 375.993 781.446 359.501 C 780.957 346.881 793.066 337.355 805.091 337.731 C 818.41 338.147 834.104 358.128 842.932 367.083 z M 1000.07 249.149 C 863.405 266.975 880.431 490.72 883 587.003 L 905.612 1500 C 906.71 1568.3 910.474 1636.48 909.733 1704.8 C 909.6 1717.06 908.313 1729.99 909.918 1742.13 C 917.153 1796.86 986.191 1789.06 1025.53 1789.05 C 1153.04 1789.04 1137.35 1770.05 1139.11 1658.17 L 1147.28 1195.18 C 1148.06 1149.2 1149.97 1103.24 1150 1057.25 C 1149.98 1042.19 1149.59 1026.99 1150.22 1011.95 L 1157.97 792.493 C 1159.03 759.009 1157.09 725.394 1158.18 691.914 C 1160.2 629.328 1162.71 566.635 1162.66 504.009 C 1162.59 404.668 1163.85 290.082 1055.87 249.149 C 1021.98 357.558 1025.2 557.101 1025.19 675.167 L 1025.18 957.997 C 1025.19 979.829 1032.44 1047.84 1014.08 1056.83 C 960.125 1083.24 976.922 960.128 976.917 936.94 L 976.916 793.449 C 976.94 645.304 975.669 500.477 986.555 352.504 C 988.472 326.449 994.847 273.341 1003.7 250.078 L 1003.11 248.212 C 1002.09 248.498 1001.05 248.738 1000.07 249.149 z M 1426.9 565.333 C 1384.07 571.213 1391.27 617.743 1389.04 650.577 C 1384.23 721.753 1377.89 868.889 1361.36 933.088 C 1342.83 1005.05 1274.84 1075.83 1197.8 1081.4 C 1197.73 1104.25 1196.38 1127.68 1197.8 1150.44 C 1210.69 1150.06 1223.33 1150.03 1236.22 1150.44 L 1237.54 1149.9 C 1254.54 1143.08 1273.66 1141.22 1291.2 1135.49 C 1415.73 1094.77 1431.57 1003.94 1441.15 894.486 L 1459.97 669.003 C 1463.16 634.628 1481.42 566.619 1426.9 565.333 z M 629.016 627.196 C 589.58 638.065 596.784 669.506 597.884 701.693 L 600.879 777.934 C 605.94 916.708 587.691 1074.42 733.354 1149.74 C 763.605 1165.39 795.051 1172.55 827.53 1181.78 C 835.004 1184.3 842.622 1186.63 850.003 1189.4 C 850.577 1165.98 848.612 1142.39 850.003 1119.02 C 785.239 1099.26 722.509 1083.83 688.232 1019.88 C 662.984 972.78 667.754 915.145 667.758 863.598 L 667.794 720.46 C 667.807 693.062 677.14 622.501 629.016 627.196 z"/>
<path transform="translate(0,0)" fill="rgb(0,0,0)" d="M 1104.17 423.893 C 1080.04 429.5 1069.03 395.67 1096.18 385.101 C 1122.56 382.917 1126.09 414.099 1104.17 423.893 z"/>
<path transform="translate(0,0)" fill="rgb(0,0,0)" d="M 942.118 504.474 C 915.91 508.49 910.745 478.676 931.282 467.141 C 957.589 465.136 964.963 493.7 942.118 504.474 z"/>
<path transform="translate(0,0)" fill="rgb(0,0,0)" d="M 1116.68 585.923 C 1091.6 590.586 1078.35 562.531 1101.41 549.204 C 1127.27 546.012 1138.67 570.631 1116.68 585.923 z"/>
<path transform="translate(0,0)" fill="rgb(0,0,0)" d="M 936.061 678.411 C 911.656 678.93 903.706 650.166 925.998 640.062 C 954.309 638.324 955.396 665.114 936.061 678.411 z"/>
<path transform="translate(0,0)" fill="rgb(0,0,0)" d="M 1105.81 797.484 C 1081.6 796.199 1074.71 765.614 1101.41 760.217 C 1127.83 761.399 1127.7 789.361 1105.81 797.484 z"/>
<path transform="translate(0,0)" fill="rgb(0,0,0)" d="M 1104.17 976.94 C 1076.84 979.623 1073.51 948.569 1096.18 939.075 C 1121.83 940.964 1125.48 965.965 1104.17 976.94 z"/>
<path transform="translate(0,0)" fill="rgb(0,0,0)" d="M 994.977 1204.86 C 968.535 1207.92 964.206 1178.75 985.142 1168 C 1012.01 1165.96 1017.13 1193.9 994.977 1204.86 z"/>
<path transform="translate(0,0)" fill="rgb(0,0,0)" d="M 1086.04 1320.19 C 1057.74 1320.01 1054.41 1288.03 1080.59 1282.18 C 1105.04 1283.35 1107.96 1312.9 1086.04 1320.19 z"/>
<path transform="translate(0,0)" fill="rgb(0,0,0)" d="M 985.142 1420.12 C 958.391 1419.75 956.808 1388.64 980.77 1382.08 C 1005.94 1384.96 1007.14 1412.25 985.142 1420.12 z"/>
</svg>`;

        const cloudContent = `
            <svg version="1.1" xmlns="http://www.w3.org/2000/svg" style="display: block;" viewBox="0 0 2048 2048" width="512" height="512">
<path transform="translate(0,0)" fill="rgb(0,0,0)" d="M 481.961 861.222 C 484.35 843.354 485.691 825.609 489.495 807.929 C 522.58 654.125 664.269 537.954 821.61 536.008 C 923.333 521.679 1036.39 595.322 1096.8 671.715 C 1190.05 562.095 1365.08 579.322 1436.81 702.135 C 1532.02 697.162 1602.6 768.628 1620.29 858.549 C 1732.45 888.948 1825.73 997.297 1821.79 1116.16 C 1816.37 1279.99 1685.8 1387.63 1525.57 1382.01 C 1456.9 1464 1334.54 1521.07 1228.21 1513.9 C 1158.33 1514.66 1077.63 1483.75 1024.6 1437.59 C 888.541 1546.89 696.359 1536.52 567.303 1422.14 C 552.251 1408.8 538.737 1393.86 525.128 1379.08 C 516.083 1379.31 507.034 1379.55 497.987 1379.64 C 351.805 1381.1 228.427 1262.74 227.924 1115.75 C 227.435 972.775 338.921 860.439 481.961 861.222 z M 810.943 578.166 C 662.103 589.784 536.962 706.612 525.887 857.217 C 520.884 925.253 512.608 901.46 455.836 905.446 C 346.887 913.097 266.844 1015.67 270.452 1121.79 C 274.388 1237.6 376.871 1340.97 494.695 1336.27 C 572.691 1333.15 535.702 1334 585.265 1380.4 C 696.332 1484.37 856.43 1502.69 982.565 1415.16 C 1046.55 1370.76 1016.84 1389.91 1079.79 1428.26 C 1198.45 1500.55 1342.87 1481.78 1448.94 1397.24 C 1464.14 1384.75 1498.39 1345.25 1510.94 1339.97 C 1527.49 1337.98 1544.07 1341.58 1560.64 1340.35 C 1689.06 1330.79 1792.08 1223.19 1779.67 1092.53 C 1763.72 924.576 1552.77 833.681 1411.78 931.509 C 1402.69 937.818 1394.72 947.786 1384.41 951.863 C 1363.01 960.327 1334.63 932.294 1389.15 895.385 C 1447.78 855.69 1507.8 846.799 1576.26 848.841 C 1560.94 796.618 1515.25 748.022 1458.37 744.878 C 1394.94 741.372 1428.95 758.183 1389.86 707.134 C 1323.73 620.778 1194.95 618.76 1126.49 701.756 C 1084.78 752.328 1085.99 723.677 1055.48 687.388 C 991.182 610.936 910.653 575.355 810.943 578.166 z"/>
<path transform="translate(0,0)" fill="rgb(0,0,0)" d="M 619.83 816.716 C 616.055 829.048 616.259 845.626 609.367 856.372 C 597.915 874.227 564.524 869.012 573.974 825.483 C 598.207 713.846 688.45 639.415 800.898 624.94 C 818.827 623.683 879.092 620.921 889.62 640.239 C 896.436 652.744 887.885 668.03 874.364 670.662 C 863.888 672.7 851.46 668.814 840.874 667.749 C 741.473 657.749 651.182 723.625 619.83 816.716 z"/>
<path transform="translate(0,0)" fill="rgb(0,0,0)" d="M 777.55 919.076 C 796.611 879.427 828.431 850.216 874.825 848.841 C 937.566 831.638 1034.44 919.125 987.277 940.725 C 966.986 950.019 956.339 924.323 944.722 912.775 C 905.558 873.841 832.326 885.246 814.764 941.1 C 800.501 986.463 798.498 988.079 758.671 958.934 C 712.307 925.007 634.979 951.466 627.664 1011.24 C 625.286 1030.68 631.303 1041.18 609.876 1050.14 C 588.601 1055.56 568.53 1022.77 537.502 1041.24 C 514.318 1055.04 521.859 1073.92 509.041 1084.97 C 462.202 1125.34 445.515 977.243 586.694 995.753 C 617.051 907.393 695.538 879.437 777.55 919.076 z"/>
<path transform="translate(0,0)" fill="rgb(0,0,0)" d="M 1623.88 1124.73 C 1613.34 1068.75 1604.63 1028.99 1536.95 1011.66 C 1527.44 1009.22 1516.68 1009.19 1507.5 1005.84 C 1486.57 998.202 1493.65 972.983 1510.94 966.262 C 1620.13 960.568 1702.3 1112.99 1648.78 1136.44 C 1637.82 1137.82 1630.81 1133.07 1623.88 1124.73 z"/>
<path transform="translate(0,0)" fill="rgb(0,0,0)" d="M 491.82 1286.92 C 387.345 1300.62 291.123 1140.69 345.383 1124.73 C 385.946 1116.97 364.99 1216.5 462.696 1241.89 C 474.27 1244.9 487.095 1245.15 498.115 1249.93 C 515.964 1257.67 506.762 1281.68 491.82 1286.92 z"/>
<path transform="translate(0,0)" fill="rgb(0,0,0)" d="M 1401.69 1339.97 C 1364.56 1345.06 1378.45 1295.36 1411.15 1286.92 C 1447.85 1280.11 1439.07 1327.36 1401.69 1339.97 z"/>
<path transform="translate(0,0)" fill="rgb(0,0,0)" d="M 1283.82 1389.02 C 1269.97 1389.14 1255.43 1377.24 1261.36 1362.2 C 1267.04 1347.79 1303.35 1343.11 1321 1335.98 C 1337.27 1336.68 1350.81 1352.55 1339.77 1367.98 C 1329.17 1382.81 1300.49 1386.61 1283.82 1389.02 z"/>
</svg>`;

        // 创建并加载图片
        Promise.all([
            this.loadImageFile('konglong.png'),  // 使用 PNG 图片替代 SVG
            this.createImage(cactusContent),
            this.createImage(cloudContent)
        ]).then(([dinoImg, cactusImg, cloudImg]) => {
            this.dinoImg = dinoImg;
            this.cactusImg = cactusImg;
            this.cloudImg = cloudImg;
            this.assetsLoaded = true;
            this.init();
        }).catch(error => {
            console.error('加载资源失败:', error);
        });
    }

    // 添加新的图片加载方法
    loadImageFile (filename) {
        return new Promise((resolve, reject) => {
            const img = new Image();
            img.onload = () => resolve(img);
            img.onerror = () => reject(new Error(`加载图片失败: ${filename}`));
            img.src = filename;  // 直接使用文件名作为路径
        });
    }

    createImage (svgContent) {
        return new Promise((resolve, reject) => {
            const blob = new Blob([svgContent], { type: 'image/svg+xml' });
            const url = URL.createObjectURL(blob);
            const img = new Image();

            img.onload = () => {
                URL.revokeObjectURL(url);
                resolve(img);
            };

            img.onerror = () => {
                URL.revokeObjectURL(url);
                reject(new Error('图片加载失败'));
            };

            img.src = url;
        });
    }

    init () {
        // 初始化云朵
        this.initClouds();
        // 开始游戏循环
        this.gameLoop();
    }

    initClouds () {
        for (let i = 0; i < DinoGame.CLOUD_COUNT; i++) {
            this.clouds.push({
                x: Math.random() * this.canvas.width,
                y: Math.random() * (DinoGame.CLOUD_MAX_Y - DinoGame.CLOUD_MIN_Y) + DinoGame.CLOUD_MIN_Y,
                speed: Math.random() * (DinoGame.CLOUD_MAX_SPEED - DinoGame.CLOUD_MIN_SPEED) + DinoGame.CLOUD_MIN_SPEED
            });
        }
    }

    // 修改点击处理方法
    handleClick (event) {
        // 确保点击事件是在canvas上触发的
        const rect = this.canvas.getBoundingClientRect();
        const clickX = event.clientX - rect.left;
        const clickY = event.clientY - rect.top;

        if (this.isGameOver) {
            // 检查是否点击了重启按钮
            if (clickX >= this.restartBtn.x &&
                clickX <= this.restartBtn.x + this.restartBtn.width &&
                clickY >= this.restartBtn.y &&
                clickY <= this.restartBtn.y + this.restartBtn.height) {
                this.restart();
            }
        } else if (clickX >= 0 && clickX <= this.canvas.width &&
            clickY >= 0 && clickY <= this.canvas.height) {
            // 确保点击在canvas范围内才触发跳跃
            this.jump();
        }
    }

    // 修改触摸处理方法
    handleTouch (event) {
        event.preventDefault();

        const rect = this.canvas.getBoundingClientRect();
        const touchX = event.touches[0].clientX - rect.left;
        const touchY = event.touches[0].clientY - rect.top;

        if (this.isGameOver) {
            if (touchX >= this.restartBtn.x &&
                touchX <= this.restartBtn.x + this.restartBtn.width &&
                touchY >= this.restartBtn.y &&
                touchY <= this.restartBtn.y + this.restartBtn.height) {
                this.restart();
            }
        } else if (touchX >= 0 && touchX <= this.canvas.width &&
            touchY >= 0 && touchY <= this.canvas.height) {
            this.jump();
        }
    }

    // 修改跳跃方法
    handleJump (event) {
        if (event.code === 'Space') {
            this.jump();
        }
    }

    // 添加统一的跳跃方法
    jump () {
        if (!this.dino.jumping && !this.isGameOver) {
            this.dino.jumping = true;
            this.dino.velocity = DinoGame.JUMP_VELOCITY;  // 使用定义的跳跃速度常量
            this.jumpScored = false;

            // 初始化音频（第一次交互时）
            if (!this.audioInitialized) {
                this.initializeAudio();
            }

            // 播放跳跃音效
            this.playJumpSound();
        }
    }

    // 更新游戏状态
    update () {
        if (this.isGameOver) return;

        // 更新恐龙位置
        if (this.dino.jumping) {
            this.dino.y += this.dino.velocity;
            this.dino.velocity += DinoGame.GRAVITY;  // 使用定义的重力常量

            if (this.dino.y >= this.canvas.height - DinoGame.DINO_GROUND_OFFSET) {
                this.dino.y = this.canvas.height - DinoGame.DINO_GROUND_OFFSET;
                this.dino.jumping = false;
                if (!this.jumpScored) {
                    this.score++;
                    this.jumpScored = true;
                }
            }
        }

        // 更新云朵位置
        this.clouds.forEach(cloud => {
            cloud.x -= cloud.speed;
            if (cloud.x + DinoGame.CLOUD_WIDTH < 0) {
                cloud.x = this.canvas.width;
                cloud.y = Math.random() * (DinoGame.CLOUD_MAX_Y - DinoGame.CLOUD_MIN_Y) + DinoGame.CLOUD_MIN_Y;
            }
        });

        // 修改速度更新逻辑
        const gameTime = (Date.now() - this.startTime) / 1000;
        this.gameSpeed = Math.min(
            DinoGame.INITIAL_GAME_SPEED + Math.floor(gameTime / DinoGame.SPEED_INCREMENT_INTERVAL) * DinoGame.SPEED_INCREMENT,
            DinoGame.MAX_GAME_SPEED
        );

        // 更新障碍物生成逻辑
        this.lastObstacleTime++;
        this.minObstacleInterval = Math.max(DinoGame.OBSTACLE_MIN_INTERVAL, 60 - (this.gameSpeed - DinoGame.INITIAL_GAME_SPEED) * 3);

        if (this.lastObstacleTime > this.minObstacleInterval) {
            if (Math.random() < DinoGame.OBSTACLE_SPAWN_CHANCE) {
                this.generateObstacleGroup();
                this.lastObstacleTime = 0;
            }
        }

        // 更新障碍物位置
        this.obstacles.forEach(obstacle => {
            obstacle.x -= this.gameSpeed;
        });

        // 移除超出画面的障碍物
        this.obstacles = this.obstacles.filter(obstacle => obstacle.x + obstacle.width > 0);

        // 碰撞检测
        this.checkCollisions();
    }

    // 生成障碍物组
    generateObstacleGroup() {
        const random = Math.random();
        let obstacleCount;
        
        // 决定障碍物数量
        if (random < 0.6) {
            obstacleCount = 1;        // 60% 概率生成1个
        } else if (random < 0.9) {
            obstacleCount = 2;        // 30% 概率生成2个
        } else {
            obstacleCount = 3;        // 10% 概率生成3个
        }

        // 生成障碍物组
        for (let i = 0; i < obstacleCount; i++) {
            // 随机生成高度
            const height = Math.random() * 
                (DinoGame.OBSTACLE_MAX_HEIGHT - DinoGame.OBSTACLE_MIN_HEIGHT) + 
                DinoGame.OBSTACLE_MIN_HEIGHT;

            this.obstacles.push({
                x: this.canvas.width + (i * (DinoGame.OBSTACLE_WIDTH + DinoGame.OBSTACLE_GROUP_SPACING)),
                y: this.canvas.height - height - DinoGame.GROUND_HEIGHT,
                width: DinoGame.OBSTACLE_WIDTH,
                height: height
            });
        }
    }

    // 碰撞检测
    checkCollisions () {
        this.obstacles.forEach(obstacle => {
            const dinoRight = this.dino.x + this.dino.width - DinoGame.COLLISION_MARGIN_X;
            const dinoLeft = this.dino.x + DinoGame.COLLISION_MARGIN_X;
            const dinoBottom = this.dino.y + this.dino.height - DinoGame.COLLISION_MARGIN_Y;
            const dinoTop = this.dino.y + DinoGame.COLLISION_MARGIN_Y;

            const obstacleRight = obstacle.x + obstacle.width - DinoGame.COLLISION_MARGIN_X;
            const obstacleLeft = obstacle.x + DinoGame.COLLISION_MARGIN_X;
            const obstacleBottom = obstacle.y + obstacle.height - DinoGame.COLLISION_MARGIN_Y;
            const obstacleTop = obstacle.y + DinoGame.COLLISION_MARGIN_Y;

            if (dinoLeft < obstacleRight &&
                dinoRight > obstacleLeft &&
                dinoTop < obstacleBottom &&
                dinoBottom > obstacleTop) {
                this.gameOver();
            }
        });
    }

    // 游戏结束
    gameOver () {
        this.isGameOver = true;
        // 停止背景音乐
        this.bgm.pause();
        this.bgm.currentTime = 0;
    }

    // 添加重启游戏方法
    restart () {
        // 重置游戏状态
        this.isGameOver = false;
        this.score = 0;
        this.gameSpeed = DinoGame.INITIAL_GAME_SPEED;
        this.lastObstacleTime = 0;
        this.minObstacleInterval = DinoGame.OBSTACLE_MIN_INTERVAL;

        // 重置恐龙位置时使用静态常量
        this.dino = {
            x: DinoGame.DINO_START_X,
            y: this.canvas.height - DinoGame.DINO_GROUND_OFFSET,
            width: DinoGame.DINO_WIDTH,
            height: DinoGame.DINO_HEIGHT,
            jumping: false,
            velocity: 0
        };

        // 清空障碍物
        this.obstacles = [];

        // 重新初始化云朵
        this.clouds = [];
        this.initClouds();

        // 重置游戏开始时间
        this.startTime = Date.now();
        // 重置跳跃计分标记
        this.jumpScored = false;

        // 如果音频已初始化，才重新播放背景音乐
        if (this.audioInitialized) {
            this.playBGM();
        }
    }

    // 绘制游戏画面
    draw () {
        if (!this.assetsLoaded) return;  // 如果资源未加载完成，不进行绘制

        this.ctx.clearRect(0, 0, DinoGame.CANVAS_WIDTH, DinoGame.CANVAS_HEIGHT);

        // 绘制云朵
        this.clouds.forEach(cloud => {
            this.ctx.drawImage(this.cloudImg, cloud.x, cloud.y, DinoGame.CLOUD_WIDTH, DinoGame.CLOUD_HEIGHT);
        });

        // 绘制地面
        this.ctx.beginPath();
        this.ctx.moveTo(0, this.canvas.height - DinoGame.GROUND_HEIGHT);
        this.ctx.lineTo(this.canvas.width, this.canvas.height - DinoGame.GROUND_HEIGHT);
        this.ctx.strokeStyle = '#000';
        this.ctx.stroke();

        // 绘制恐龙
        this.ctx.drawImage(this.dinoImg, this.dino.x, this.dino.y, this.dino.width, this.dino.height);

        // 绘制障碍物
        this.obstacles.forEach(obstacle => {
            this.ctx.drawImage(this.cactusImg,
                obstacle.x,
                this.canvas.height - DinoGame.OBSTACLE_GROUND_OFFSET - obstacle.height,
                obstacle.width,
                obstacle.height
            );
        });

        // 绘制分数和速度
        this.ctx.font = '20px Arial';
        this.ctx.fillStyle = '#000';
        this.ctx.fillText(`得分: ${this.score}`, 10, 30);
        this.ctx.fillText(`速度: ${this.gameSpeed.toFixed(1)}`, 10, 60);

        if (this.isGameOver) {
            // 绘制游戏结束文字
            this.ctx.font = '40px Arial';
            this.ctx.fillStyle = '#000';
            this.ctx.fillText('GAME OVER', this.canvas.width / 2 - 100, this.canvas.height / 2 - 20);

            // 绘制重启按钮
            this.ctx.fillStyle = '#4CAF50';
            this.ctx.fillRect(
                this.restartBtn.x,
                this.restartBtn.y,
                this.restartBtn.width,
                this.restartBtn.height
            );

            // 绘制按钮文字
            this.ctx.font = '20px Arial';
            this.ctx.fillStyle = '#fff';
            this.ctx.textAlign = 'center';
            this.ctx.textBaseline = 'middle';
            this.ctx.fillText(
                '重新开始',
                this.restartBtn.x + this.restartBtn.width / 2,
                this.restartBtn.y + this.restartBtn.height / 2
            );
            this.ctx.textAlign = 'left';  // 重置文本对齐方式
            this.ctx.textBaseline = 'alphabetic';  // 重置文本基线
        }
    }

    // 游戏主循环
    gameLoop () {
        this.update();
        this.draw();
        requestAnimationFrame(this.gameLoop.bind(this));
    }

    // 修改音频加载方法
    loadAudioAssets () {
        // 加载背景音乐
        this.bgm = new Audio(DinoGame.BGM_PATH);
        this.bgm.loop = true;

        // 加载跳跃音效
        this.jumpSound = new Audio(DinoGame.JUMP_SOUND_PATH);

        // 设置音频加载完成的处理
        Promise.all([
            this.loadAudio(this.bgm),
            this.loadAudio(this.jumpSound)
        ]).then(() => {
            console.log('音频加载完成');
            // 不要自动播放，等待用户交互
        }).catch(error => {
            console.error('音频加载失败:', error);
        });
    }

    // 修改音频加载Promise包装方法
    loadAudio (audio) {
        return new Promise((resolve, reject) => {
            audio.addEventListener('canplaythrough', resolve, { once: true });
            audio.addEventListener('error', reject);
        });
    }

    // 添加背景音乐播放方法
    playBGM () {
        // 尝试播放背景音乐
        this.bgm.play().catch(error => {
            console.error('背景音乐播放失败:', error);
        });
    }

    // 添加跳跃音效播放方法
    playJumpSound () {
        // 重置音效播放位置并播放
        this.jumpSound.currentTime = 0;
        this.jumpSound.play().catch(error => {
            console.error('跳跃音效播放失败:', error);
        });
    }

    // 添加音频初始化方法
    initializeAudio () {
        if (!this.audioInitialized) {
            this.playBGM();
            this.audioInitialized = true;
        }
    }
} 